package com.ilop.sthome.common.udp;

import android.text.TextUtils;

import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.bean.GatewayBean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author skygge
 * @date 2020/5/13.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class GatewayUdpUtil {
    private static final String TAG = GatewayUdpUtil.class.getSimpleName();

    private static volatile GatewayUdpUtil instance = null;

    private List<DeviceInfoBean> mDeviceInfoList;

    private List<GatewayBean> mGatewayList;

    private String mCurrentGateway = null;

    private boolean receiveDataOrNot=false;

    public GatewayUdpUtil(){
        mGatewayList = new ArrayList<>();
    }

    public static GatewayUdpUtil getInstance(){
        if (instance == null){
            synchronized (GatewayUdpUtil.class){
                if (instance == null){
                    instance = new GatewayUdpUtil();
                }
            }
        }
        return instance;
    }

    public static void setInstance(){
        instance = null;
    }

    //将获取到的网关列表直接放入到内网的对比表格之中
    public void setDeviceInfoBeans(List<DeviceInfoBean> deviceInfoBeans){
        this.mDeviceInfoList = deviceInfoBeans;
    }

    //获取当前
    boolean isReceiveDataOrNot() {
        return receiveDataOrNot;
    }

    private void setCurrentGateway(String currentGateway) {
        this.mCurrentGateway = currentGateway;
    }

    //在发送数据前将标志位设置成初始值
    private void startSendData(){
        this.receiveDataOrNot=false;
    }

    //用于网关内网的连接状态判断，没有介绍到信息，标志位将变成false
    void setReceiveDataOrNot(String deviceName, int receiveCmdId) {
        if(mCurrentGateway.equalsIgnoreCase(deviceName)){
            switch (receiveCmdId){
                case 0:
                case 11:
                case 13:
                case 17:
                case 19:
                case 22:
                case 26:
                case 27:
                case 28:
                case 43:
                case 45:
                case 48:
                    receiveDataOrNot=true;
                    break;
                default:
                    receiveDataOrNot=false;
                    break;
            }
        }else{
            receiveDataOrNot=false;
        }
    }

    //添加/覆盖 网关内网信息
    void insertGateway(GatewayBean gateway){
        int size = mGatewayList.size();
        if(size>0){
            for (int i = 0; i < size; i++) {
                GatewayBean gatewayBean = mGatewayList.get(i);
                if (gateway.getName().equalsIgnoreCase(gatewayBean.getName())) {
                    mGatewayList.remove(gatewayBean);
                    mGatewayList.add(gateway);
                    break;
                }
            }
        }else {
            mGatewayList.add(gateway);
        }
    }

    //根据deviceName 名字是唯一的
    GatewayBean checkByName(String name){
        if(mGatewayList.size()>0 && !TextUtils.isEmpty(name)){
            for(GatewayBean gatewayBean:mGatewayList){
                if (name.equalsIgnoreCase(gatewayBean.getName())) {
                    return gatewayBean;
                }
            }
        }
        return null;
    }

    //检查内网中收索到的设备，是否是该用户的设备
    boolean checkUdpDeviceBelongToUser(String name){
        if(mDeviceInfoList.size()>0){
            for(DeviceInfoBean db:mDeviceInfoList){
                if(name.equalsIgnoreCase(db.getDeviceName())){
                    return true;
                }
            }
        }
        return false;
    }

    //检查当前网关是否内网在线
    public boolean isGatewayIntranet(String deviceName){
        setCurrentGateway(deviceName);
        GatewayBean gateway = checkByName(deviceName);
        if (gateway!=null && gateway.isOnline() && gateway.getIpAddress()!=null){
            startSendData();
            return true;
        }else {
            return false;
        }
    }

    //设置当前网关内网离线
    void setGatewayOffLine(String deviceName){
        GatewayBean gateway = checkByName(deviceName);
        if (gateway!=null){
            mGatewayList.remove(gateway);
        }
    }

    //设置全部网关内网离线
    public void setAllGatewayOffLine(){
        mGatewayList.clear();
    }
}
